package com.dyh.quickdemozwei.omsutil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yhdong
 * @Date: 2021/8/12 20:02
 */
public interface DateUtil {
    Logger logger = LoggerFactory.getLogger(DateUtil.class);
//     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//     SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    String utcFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    String utcFormat2 = "yyyy-MM-dd'T00:00:00.000Z'";

    static Date silenceParse(String str, SimpleDateFormat sdf){
        Date res = null;
        try {
            res = sdf.parse(str);
        } catch (ParseException e) {

        }
        return res;
    }

    static Date str2DateM(String str) {
        if (StringUtils.isEmpty(str))
            return null;
        Date result=null;
        try {
            result = SimpleDateFormatThreadSupport.getInstance("yyyy-MM-dd HH:mm:ss.SSS").parse(str);
        } catch (Exception e) {
            //  e.printStackTrace();
        }
        return result;
    }

    static Date str2DateM2(String str) {
        if (StringUtils.isEmpty(str))
            return null;
        Date result=null;
        try {
            result = SimpleDateFormatThreadSupport.getInstance("yyyy-MM-dd").parse(str);
        } catch (Exception e) {
            //  e.printStackTrace();
        }
        return result;
    }


    static Date str2Date(String str) {
        if (StringUtils.isEmpty(str))
            return null;
        if (str.length()!=19)
            return null;
        Date result=null;
        try {
            result = SimpleDateFormatThreadSupport.getInstance().parse(str);
        } catch (Exception e) {
            //  e.printStackTrace();
        }
        return result;
    }

    static String date2str(Date date){
        if (date==null)
            return "";
        return SimpleDateFormatThreadSupport.getInstance().format(date);
    }

    static String date2str(Date date, SimpleDateFormat sdf){
        if (date==null)
            return "";
        return sdf.format(date);
    }

    static String date2utcStr(Date date){
        if (date==null)
            return "";
        return SimpleDateFormatThreadSupport.getInstance(utcFormat, TimeZone.getTimeZone("GMT")).format(date);
    }

    static String date2utcStr2(Date date){
        if (date==null)
            return "";
        return SimpleDateFormatThreadSupport.getInstance(utcFormat2, TimeZone.getTimeZone("GMT")).format(date);
    }

    static List<Date[]> splitTime(Date fullStart, Date fullEnd, long step, TimeUnit timeUnit){
        long start = fullStart.getTime();
        List<Date[]> res = new ArrayList<>();
        for (;;){
            long end = Math.min(start+timeUnit.toMillis(step),fullEnd.getTime());
            res.add(new Date[] {new Date(start),new Date(end)});
            if ((start=end) ==fullEnd.getTime())
                break;
        }
        return res;
    }

    static List<Date[]> splitTime(Date fullStart, Date fullEnd, long step, TimeUnit timeUnit, long minTime){
        long start = fullStart.getTime();
        List<Date[]> res = new ArrayList<>();
        for (;;){
            long end = Math.min(start+timeUnit.toMillis(step),fullEnd.getTime());
            if (Math.abs(end-start)<minTime)
                break;
            res.add(new Date[]{new Date(start),new Date(end)});
            if ((start=end) ==fullEnd.getTime())
                break;
        }
        return res;
    }

    static boolean equals(Date date1, Date date2) {
        if (date1 == date2) {
            return true;
        }
        if (date1 != null) {
            return date1.equals(date2);
        }
        return false;
    }

       public static void main(String[] args) {
            System.out.println("adwad");
            System.out.println(DateUtil.date2utcStr(DateUtil.str2DateM("2021-09-30 14:55:44.123")));
            System.out.println(DateUtil.date2utcStr2(DateUtil.str2DateM("2021-09-30 14:55:44.123")));
            System.out.println("xxx");
        }

//        public static void main(String[] args) {
//            System.out.println("adwad");
//            System.out.println(DateUtil.date2utcStr(DateUtil.str2DateM("2021-09-30 14:55:44.123")));
//            System.out.println("xxx");
//        }

//    public static void main(String[] args) {
//         Date start1 = DateUtil.str2Date(" ");
//         Date start2 = DateUtil.str2Date("2021-09-30 14:55:44");
//         Date start3 = DateUtil.str2Date("2021-09-30 14:55:44.09");
//        System.out.println();
//    }
//    public static void main(String[] args) {
//        Date start = DateUtil.str2Date("2021-09-27 09:55:00");
//        Date end = new Date(DateUtil.str2Date("2021-09-27 09:55:10").getTime()+1100);
//        List<TimeZone> timeZones1 = splitTime(start,end,5, TimeUnit.SECONDS,1000);
//        List<TimeZone> timeZones2 = splitTime(start,end,5, TimeUnit.SECONDS);
//
//        System.out.println();
//    }
}
